package study.springboot.mvc.controller._1_binding;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import study.eggs.result.Result;
import study.eggs.result.Results;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/binding")
public class BindingController {

    /**
     * （★）请求参数
     * （1）get请求 /requestParam?uname=wangzhj
     */
    @RequestMapping("/requestParam")
    public Result requestParam(@RequestParam(name = "uname", required = true, defaultValue = "1") String uname) {
        log.info("uname= {}", uname);
        return Results.ok();
    }

    /**
     * （★）路径参数
     * （1）与 get 或 post 提交无关
     * （2）@PathVariable中name值与映射参数name名称一致时，可省略
     */
    @RequestMapping("/pathVariable/{uname}")
    public Result pathVariable(@PathVariable(name = "uname", required = true) String uname) {
        log.info("uname= {}", uname);
        return Results.ok();
    }

    /**
     * （★）请求头部
     */
    @RequestMapping("/requestHeader")
    public Result requestHeader(@RequestHeader(name = "x-request-id", required = true, defaultValue = "111111") String requestId) {
        log.info(">>>>>> {}", requestId);
        return Results.ok();
    }

    /**
     * （★）请求Cookie
     */
    @RequestMapping("/cookieValue")
    public Result cookieValue(@CookieValue(name = "token", required = true, defaultValue = "aaa-123-bbb") String token) {
        log.info(">>>>>> {}", token);
        return Results.ok();
    }

    /**
     * （1）SpringMVC 会按照请求参数名和 POJO 属性名自动匹配自动为该对象填充属性值
     * （2）支持级联属性
     */
    @RequestMapping("/pojo")
    public Result pojo(@RequestBody Map<String, Object> param) {
        log.info("{}", param);
        return Results.ok();
    }
}
